/*______________________________________________________________________________

prepare_instrument_comtrade.do
This file reads in the comtrade 2017, 2018, and 2019 trade data (downloaded in HS2012 revision), and 
collapses the data down to the Importer Country x HS4 x Year level. Also 
calculates the share of pegged import value. 

Output of this file to be merged with estdata_xcurrencychoice.dta
______________________________________________________________________________*/


*i. For importing regs
*******************************
* Create a dataset of the 2017-2019 comtrade data collapsed down to the Importer x Year x HS4 level
* Calculate the share of pegged imports

use ../rawdata/2017_H2012_exports, clear
	append using ../rawdata/2018_H2012_exports
	append using ../rawdata/2019_H2012_exports
		
	rename *_code *_num
	rename *_iso *_iso3
	* Average trade values across the three years
	gcollapse (mean) val, by(i_* j_* hs6)
	
	* Concord iso3 codes to iso2 codes
	rename i_iso3 iso3
	merge m:1 iso3 using ../rawdata/countrycodeconcord, keep(1 3) nogen keepusing(iso2)
	rename iso2 i_iso2
	rename (iso3 j_iso3) (i_iso3 iso3)
	merge m:1 iso3 using ../rawdata/countrycodeconcord.dta, keep(1 3) nogen keepusing(iso2)
	rename iso2 j_iso2
	rename iso3 j_iso3

*drop if Belgium is importer or exporter
*drop "other" nes etc
******************************************
	drop if i_iso3=="BEL"| j_iso3=="BEL"
	drop if i_iso3=="WLD"|j_iso3=="WLD"
	drop if missing(i_iso3)|missing(j_iso3)
	
	* Merge on Peg indicator
	rename j_iso2 cty_code
	merge m:1 cty_code using ../data/usd_peg, keep(1 3) nogen
	rename cty_code j_iso2

*5 countries don't have a peg indicator: Montenegro, Myanmar, Palau, Serbia, and State of Palestine. 
*based on internet search, Palau and State of Palestine are pegged to US$ (Myanmar is pegged to IMF SDR)
	replace usd_peg=1 if i_iso3=="PLW"|i_iso3=="PSE"
	replace usd_peg=0 if missing(usd_peg)

	gen val_peg = val*(usd_peg==1)
	gen val_US = val * (j_iso3=="USA")
	gen val_CHN = val * (j_iso3=="CHN")
		
	gen hs4 = substr(hs6,1,4)
	
	gcollapse (sum) total_val = val total_peg_val = val_peg total_US=val_US total_CHN=val_CHN, by(i* hs4)
	rename i_* cty_* 
	drop if total_val==0
*drops about 7 importing countries like Bonair, Myanmar	
	drop if mi(cty_iso2)
	gen IVsh_pegged_value = total_peg_val  / total_val
	gen IVsh_US = total_US/total_val
	gen IVsh_CHN = total_CHN/total_val
	
	rename cty_iso2 cty_code
		
	keep cty_code hs4 IVsh*
	
	label var IVsh_pegged_val "Share of pegged export value for hs4 x cty"
	save ../data/2017_2019_comtrade_US_sh_imp, replace
	

*********************************************************
*for exporting regs
*************************************

* Create a dataset of the 2017-2019 comtrade data collapsed down to the Importer x Year x HS4 level
* Calculate the share of pegged imports

use ../rawdata/2017_H2012_exports.dta, clear
	append using ../rawdata/2018_H2012_exports.dta
	append using ../rawdata/2019_H2012_exports.dta
		
	rename *_code *_num
	rename *_iso *_iso3
	* Average trade values across the three years
	gcollapse (mean) val, by(i_* j_* hs6)
	
	* Concord iso3 codes to iso2 codes
	rename i_iso3 iso3
	merge m:1 iso3 using ../rawdata/countrycodeconcord.dta, keep(1 3) nogen keepusing(iso2)
	rename iso2 i_iso2
	rename (iso3 j_iso3) (i_iso3 iso3)
	merge m:1 iso3 using ../rawdata/countrycodeconcord.dta, keep(1 3) nogen keepusing(iso2)
	rename iso2 j_iso2
	rename iso3 j_iso3

*drop if Belgium is importer or exporter
*drop "other" nes etc
******************************************
	drop if i_iso3=="BEL"| j_iso3=="BEL"
	drop if i_iso3=="WLD"|j_iso3=="WLD"
	drop if missing(i_iso3)|missing(j_iso3)
	
	* Merge on Peg indicator
	rename i_iso2 cty_code
	merge m:1 cty_code using ../data/usd_peg, keepusing(usd_peg) keep(1 3) nogen
	rename cty_code i_iso2

*5 countries don't have a peg indicator: Montenegro, Myanmar, Palau, Serbia, and State of Palestine. 
*based on internet search, Palau and State of Palestine are pegged to US$ (Myanmar is pegged to IMF SDR)
	replace usd_peg=1 if i_iso3=="PLW"|i_iso3=="PSE"
	replace usd_peg=0 if missing(usd_peg)

	gen val_peg = val*(usd_peg==1)
	gen val_US = val * (i_iso3=="USA")
	gen val_CHN = val * (i_iso3=="CHN")
		
	gen hs4 = substr(hs6,1,4)
	
	gcollapse (sum) total_val = val total_peg_val = val_peg total_US=val_US total_CHN=val_CHN, by(j* hs4)
	rename j_* cty_* 

*drops about 7 importing countries like Bonair, Myanmar	
	drop if mi(cty_iso2)
	gen IVsh_pegged_value = total_peg_val  / total_val
	gen IVsh_US = total_US/total_val
	gen IVsh_CHN = total_CHN/total_val
		
	rename cty_iso2 cty_code
	keep cty_code hs4 IVsh*
		
	label var IVsh_pegged_val "Share of pegged import value for hs4 x cty"
	save ../data/2017_2019_comtrade_US_sh_exp.dta, replace
	

	